layui.config({
	base : "../static/js/"
}).extend({
    "address" : "orgedit"
})
layui.use(['form','layer','laydate','address'],function(){
    var form = layui.form
        layer = parent.layer === undefined ? layui.layer : top.layer,
        laypage = layui.laypage,
        laydate = layui.laydate,
        address = layui.address,
        $ = layui.jquery;
    
    //获取省信息
    address.provinces(tempCallingprovince,tempCallingcity,tempCalledprovince,tempCalledcity,tempRoamCity);
    
    var submitTime;
    //日期范围
    laydate.render({
      elem: '#callingTime',
      range: true,
      done : function(value, date, endDate){
          submitTime = value.toString();
      }
    });

    //格式化时间
    function filterTime(val){
        if(val < 10){
            return "0" + val;
        }else{
            return val;
        }
    }
    //定时发布
    var time = new Date();
    var submitTime = time.getFullYear()+'-'+filterTime(time.getMonth()+1)+'-'+filterTime(time.getDate())+' '+filterTime(time.getHours())+':'+filterTime(time.getMinutes())+':'+filterTime(time.getSeconds());
    laydate.render({
        elem: '#release',
        type: 'datetime',
        trigger : "click",
        done : function(value, date, endDate){
            submitTime = value;
        }
    });
    form.on("radio(release)",function(data){
        if(data.elem.title == "定时发布"){
            $(".releaseDate").removeClass("layui-hide");
            $(".releaseDate #release").attr("lay-verify","required");
        }else{
            $(".releaseDate").addClass("layui-hide");
            $(".releaseDate #release").removeAttr("lay-verify");
            submitTime = time.getFullYear()+'-'+(time.getMonth()+1)+'-'+time.getDate()+' '+time.getHours()+':'+time.getMinutes()+':'+time.getSeconds();
        }
    });

    form.verify({
    	title : function(val){
            if(val == ''){
                return "场景名称内容不能为空";
            }
        },
        ratio : function(val){
        	var reg = /^\d{1,2}$/;
            if(val != '' && !reg.test(val)){
                return "请输入正确的数值";
            }
        }
    })
    form.on("submit(editCallRatio)",function(data){
    	var id = $("#id").val();
    	var name = $("#name").val();
    	var callingProvince = data.field.callingProvince;
    	var callingCity = data.field.callingCity;
    	var roamCity = data.field.roamCity;
    	var callingSex = data.field.callingSex;
    	var callingFrequency = data.field.callingFrequency;
    	var callingTime = $("#callingTime").val().trim();
    	var priceMin = $("#priceMin").val().trim();
    	var priceMax = $("#priceMax").val().trim();
    	if(priceMin == ''){
    		priceMin = 0;
    	}
    	if(priceMax == ''){
    		priceMax = 0;
    	}
    	if(priceMin >= priceMax && priceMin > 0){
    		layer.msg('主叫占比参数填写错误,请重新填写', {icon: 2});
    		return false;
    	}
    	var calledFrequency = data.field.calledFrequency;
    	var calledProvince = data.field.calledProvince;
    	var calledCity = data.field.calledCity;
    	var calledSex = data.field.calledSex;
    	var calledAge = data.field.calledAge;
        //弹出loading
        var index = top.layer.msg('数据提交中，请稍候',{icon: 16,time:false,shade:0.8});
        setTimeout(function(){
        	$.ajax({
    			type : "POST",
    			url : "../callRatio/edit",
    			data : {
    				"id" : id,
    				"name" : name,
    				"callingProvince" : callingProvince,
    				"callingCity" : callingCity,
    				"roamCity" : roamCity,
    				"callingSex" : callingSex,
    				"callingFrequency" : callingFrequency,
    				"callingTime" : callingTime,
    				"priceMin" : priceMin,
    				"priceMax" : priceMax,
    				"calledFrequency" : calledFrequency,
    				"calledProvince" : calledProvince,
    				"calledCity" : calledCity,
    				"calledSex" : calledSex,
    				"calledAge" : calledAge
    			},
    			dataType : "json",
    			success : function(jsonObject) {
    				top.layer.close(index);
    	            top.layer.msg("场景修改成功！");
    	            layer.closeAll("iframe");
    	            //刷新父页面
    	            parent.location.reload();
    			}
    		});
        },2000);
        return false;
    })

    //预览
    form.on("submit(look)",function(){
        layer.alert("此功能需要前台展示，实际开发中传入对应的必要参数进行文章内容页面访问");
        return false;
    })
})